﻿Imports System.IO
Imports System.Globalization

Partial Public Class Sum_Money_AllMat
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "Monthly Report"
        End If
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub radioPOrt_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioPOrt.SelectedIndexChanged
        If (radioPOrt.SelectedValue = "B") Then
            rowDestination.Visible = False
        Else
            rowDestination.Visible = True
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        If (txtDate.Text.Trim() = "") Or (txtDateTo.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุช่วงเวลา');", True)
            Exit Sub
        End If
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet

        LoadData()
        dsReport.Tables.Add(ViewState("Sum"))
        dsReport.AcceptChanges()
        If (radioPOrt.SelectedValue = "B") Then
            ReportName = "Mat_all_report_Bulk.rdlc"
            reportDataSource = "Monthly_Bulk_Monthly"
        Else
            ReportName = "Mat_all_report.rdlc"
            reportDataSource = "Monthly_Monthly"
        End If

        paraName = "DateTimeNow"
        paraValue = DateTime.Now.ToString()
        Orientation = False
        reportType = radioType.SelectedValue

        urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

        Dim oldFile As String = String.Empty
        oldFile = Server.MapPath(urlDisplay)
        Dim fileInfo As FileInfo = New FileInfo(oldFile)
        Dim reportPath As String = "../WriteReport/"

        Dim newFileName As String = DateTime.Now.ToString("_yyMMdd_HHmmss", New CultureInfo("en-US"))
        newFileName = "Monthly_Report" & newFileName

        If fileInfo.Exists Then
            fileInfo.MoveTo(fileInfo.DirectoryName & "/" & newFileName & fileInfo.Extension)
            urlDisplay = reportPath & newFileName & fileInfo.Extension
        End If

        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
    End Sub

    Private Sub LoadData()
        Dim dt As DataTable = clsGet.Get_Booking_For_MAT_FREE_REPORT(radioDestination.SelectedValue, CDate(txtDate.Text).ToString("yyyyMMdd"), CDate(txtDateTo.Text).ToString("yyyyMMdd"), radioPOrt.SelectedValue, "")
        ViewState("Sum") = Create()
        If (radioPOrt.SelectedValue = "B") Then
            Dim dtBulk As DataTable = ViewState("Sum")
            dtBulk.Columns.Add("Admis_Fee", GetType(String))
            dtBulk.Columns.Add("Weighting", GetType(String))
            dtBulk.Columns.Add("Hopper", GetType(String))
            dtBulk.Columns.Add("WB", GetType(String))
            dtBulk.Columns.Add("Other", GetType(String))
            dtBulk.AcceptChanges()
            ViewState("Sum") = dtBulk
        End If
        If (dt.Rows.Count > 0) Then
            Dim Booking_no As String = ""
            For Each dr As DataRow In dt.Rows
                If (dr.Item("Booking_no") & "" <> "") Then
                    Dim Owner As String = ""
                    Dim _Blank As Boolean = False
                    If (dr.Item("PORT") & "" = "B") Then
                        Owner = dr.Item("MAgent_No")
                        _Blank = True
                    End If
                    Dim dtMat As DataTable = clsGet.Get_Booking_For_MAT_FREE_REPORT_ItemMonthly(dr.Item("Booking_no"), Owner)
                    If (dtMat.Rows.Count > 0) Then
                        Dim _184 As Boolean = True
                        Dim _200 As Boolean = True
                        Dim _001 As Boolean = True
                        Dim _268 As Boolean = True
                        Dim _236 As Boolean = True
                        Dim _237 As Boolean = True
                        For Each dx As DataRow In dtMat.Rows
                            If dx.Item("MATERIAL") & "" = "000000002100000184" Then
                                _184 = False
                            End If
                            If dx.Item("MATERIAL") & "" = "000000002100000200" Then
                                _200 = False
                            End If
                            If dx.Item("MATERIAL") & "" = "000000002100000001" Then
                                _001 = False
                            End If
                            If dx.Item("MATERIAL") & "" = "000000002100000236" Then
                                _236 = False
                                _237 = False
                            ElseIf dx.Item("MATERIAL") & "" = "000000002100000237" Then
                                _237 = False
                            End If
                            If (dr.Item("PORT") = "L") Then
                                If dx.Item("MATERIAL") & "" = "000000002100000281" Then
                                    _268 = False
                                End If
                            Else
                                ' If dx.Item("MATERIAL") & "" = "000000002100000268" Then
                                _268 = False
                                '  End If
                            End If

                        Next
                        'If ((_184) Or (_200) Or (_001) Or (_268)) Then
                        AddData(dtMat, dr.Item("Booking_no"), dr.Item("Vehicle_Text"), dr.Item("Vehicle_GRT"), dr.Item("Vehicle_DWT"), dr.Item("Vehicle_LOA"), dr.Item("Vehicle_Draft"), dr.Item("PORT"), dr.Item("MAgent_no"), _184, _200, _001, _268, dr.Item("MAgent_name"), True, False, False, False, _236, _237, _Blank)
                        'End If
                    Else
                        Dim _268 As Boolean = True
                        If (dr.Item("PORT") & "" = "B") Then
                            _268 = False
                        End If
                        AddData(dtMat, dr.Item("Booking_no"), dr.Item("Vehicle_Text"), dr.Item("Vehicle_GRT"), dr.Item("Vehicle_DWT"), dr.Item("Vehicle_LOA"), dr.Item("Vehicle_Draft"), dr.Item("PORT"), dr.Item("MAgent_no"), True, True, True, _268, dr.Item("MAgent_name"), True, False, False, False, True, True, _Blank)
                    End If
                    If (dr.Item("PORT") & "" = "B") Then
                        _Blank = False
                        Dim dtOwner As DataTable = clsGet.Get_Booking_Product(dr.Item("Booking_no"))
                        If (dtOwner.Rows.Count > 0) Then
                            For Each dow As DataRow In dtOwner.Rows
                                If (dow.Item("Owner_Product_no") & "" <> "") Then
                                    dtMat = clsGet.Get_Booking_For_MAT_FREE_REPORT_ItemMonthly(dr.Item("Booking_no"), dow.Item("Owner_Product_no"))
                                    If (dtMat.Rows.Count > 0) Then
                                        Dim _184 As Boolean = True
                                        Dim _200 As Boolean = True
                                        Dim _001 As Boolean = True
                                        Dim _268 As Boolean = True
                                        Dim _100 As Boolean = True
                                        Dim _255 As Boolean = True
                                        Dim _104 As Boolean = True
                                        For Each dx As DataRow In dtMat.Rows
                                            If dx.Item("MATERIAL") & "" = "000000002100000184" Then
                                                _184 = False
                                            End If
                                            If dx.Item("MATERIAL") & "" = "000000002100000200" Then
                                                _200 = False
                                            End If
                                            If dx.Item("MATERIAL") & "" = "000000002100000001" Then
                                                _001 = False
                                            End If
                                            If dx.Item("MATERIAL") & "" = "000000002100000255" Then
                                                _255 = False
                                            End If
                                            If (dx.Item("MATERIAL") & "" = "000000002100000100") Or (dx.Item("MATERIAL") & "" = "000000002100000099") Or (dx.Item("MATERIAL") & "" = "000000002100000098") Or (dx.Item("MATERIAL") & "" = "000000002100000097") Then
                                                _100 = False
                                            End If
                                            If (dr.Item("PORT") = "L") Then
                                                If dx.Item("MATERIAL") & "" = "000000002100000281" Then
                                                    _268 = False
                                                End If
                                            Else
                                                If dx.Item("MATERIAL") & "" = "000000002100000268" Then
                                                    _268 = False
                                                End If
                                            End If

                                        Next
                                        ' If ((_184) Or (_200) Or (_001) Or (_268)) Then
                                        AddData(dtMat, dr.Item("Booking_no"), dr.Item("Vehicle_Text"), dr.Item("Vehicle_GRT"), dr.Item("Vehicle_DWT"), dr.Item("Vehicle_LOA"), dr.Item("Vehicle_Draft"), dr.Item("PORT"), dr.Item("MAgent_no"), _184, _200, _001, _268, dow.Item("Owner_Product_name"), False, _100, _255, _104, False, False, _Blank)
                                        ' End If
                                    Else
                                        AddData(dtMat, dr.Item("Booking_no"), dr.Item("Vehicle_Text"), dr.Item("Vehicle_GRT"), dr.Item("Vehicle_DWT"), dr.Item("Vehicle_LOA"), dr.Item("Vehicle_Draft"), dr.Item("PORT"), dr.Item("MAgent_no"), True, True, True, True, dow.Item("Owner_Product_name"), False, True, True, True, False, False, _Blank)
                                    End If
                                End If
                            Next

                        End If
                    End If



                End If
            Next
            dt = ViewState("Sum")
            If (dt.Rows.Count < 1) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่พบข้อมูล ');", True)
            End If

        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่พบข้อมูล ');", True)
        End If

    End Sub

    Private Sub AddData(ByVal dtValue As DataTable, ByVal booking_no As String, ByVal Vehicle_Text As String, ByVal GRT As String, ByVal DWT As String, ByVal LOA As String, ByVal Draft As String, ByVal PORT As String, ByVal Customer As String, ByVal _184 As Boolean, ByVal _200 As Boolean, ByVal _001 As Boolean, ByVal _268 As Boolean, ByVal Agent As String, ByVal ShipAgent As Boolean, ByVal _100 As Boolean, ByVal _255 As Boolean, ByVal _104 As Boolean, ByVal _236 As Boolean, ByVal _237 As Boolean, ByVal _Blank As Boolean)
        Dim dt As DataTable = ViewState("Sum")
        Dim dr As DataRow = dt.NewRow
        Dim product_Amount As Double = 0.0
        Dim Product_Load As String = ""
        Dim Product_Disch As String = ""
        Dim Product_Bulk As Double = 0.0
        Dim L_D As String = ""
        dr.Item("Booking_no") = booking_no
        dr.Item("Ship_name") = Vehicle_Text
        dr.Item("Agent") = Agent
        dr.Item("LOA") = LOA
        dr.Item("GRT") = GRT
        dr.Item("DWT") = DWT
        Dim dtProduct As DataTable = clsGet.Get_Booking_Product(booking_no)
        If (dtProduct.Rows.Count > 0) Then
            L_D = dtProduct.Rows(0).Item("Product_Event") & ""
        End If
        dtProduct = clsGet.Get_Load_Unload_Product(booking_no, "")
        For Each dm As DataRow In dtProduct.Rows
            If (L_D = "L") Then
                If Product_Load = "" Then
                    Product_Load = dm.Item("Product_name")
                Else
                    Product_Load &= "," & dm.Item("Product_name")
                End If
            Else
                If Product_Disch = "" Then
                    Product_Disch = dm.Item("Product_name")
                Else
                    Product_Disch &= "," & dm.Item("Product_name")
                End If
            End If
            Try
                If (PORT = "L") Then
                    product_Amount += CDbl(dm.Item("LU_BLMT"))
                Else
                    If (dm.Item("BU_Truck") & "" = "") Then
                        product_Amount += CDbl(dm.Item("BU_Bill"))
                    Else
                        product_Amount += CDbl(dm.Item("BU_Truck"))
                    End If
                    If (dm.Item("BU_Bill") & "" = "") Then
                        Product_Bulk += dm.Item("BU_Manifest")
                    Else
                        Product_Bulk += dm.Item("BU_Bill")
                    End If
                End If

            Catch ex As Exception

            End Try

        Next
        Dim Shifting As Boolean = False
        Dim BookingShifting As String = ""
        If Not (_Blank) Then
            Dim dtWharf As DataTable = clsGet.Get_BookingWharf(booking_no)
            If (dtWharf.Rows.Count > 0) Then
                If (dtWharf.Rows(0).Item("Shift_Booking") & "" <> "") Then
                    Shifting = True
                    BookingShifting = dtWharf.Rows(0).Item("Shift_Booking")
                End If
            End If
            If (Shifting) Then
                dtProduct = clsGet.Get_Load_Unload_Product(BookingShifting, "")
                For Each dm As DataRow In dtProduct.Rows
                    If (L_D = "L") Then
                        If Product_Load = "" Then
                            Product_Load = dm.Item("Product_name")
                        Else
                            Product_Load &= "," & dm.Item("Product_name")
                        End If
                    Else
                        If Product_Disch = "" Then
                            Product_Disch = dm.Item("Product_name")
                        Else
                            Product_Disch &= "," & dm.Item("Product_name")
                        End If
                    End If
                    Try
                        If (PORT = "L") Then
                            product_Amount += CDbl(dm.Item("LU_BLMT"))
                        Else
                            If (dm.Item("BU_Truck") & "" = "") Then
                                product_Amount += CDbl(dm.Item("BU_Bill"))
                            Else
                                product_Amount += CDbl(dm.Item("BU_Truck"))
                            End If
                            If (dm.Item("BU_Bill") & "" = "") Then
                                Product_Bulk += dm.Item("BU_Manifest")
                            Else
                                Product_Bulk += dm.Item("BU_Bill")
                            End If
                        End If

                    Catch ex As Exception

                    End Try

                Next
            End If
            dr.Item("Load") = Product_Load
            dr.Item("Disch") = Product_Disch
            If (PORT = "B") Then
                dr.Item("MT") = Product_Bulk
            Else
                dr.Item("MT") = product_Amount
            End If

        End If


        Dim dtActual As DataTable = clsGet.GetActualTIme(booking_no)
        Dim dtLog_Out As DataTable
        If (Shifting) Then
            dtLog_Out = clsGet.Get_Booking_LogSheet_Out(BookingShifting)
        Else
            dtLog_Out = clsGet.Get_Booking_LogSheet_Out(booking_no)
        End If


        Dim Berthing_Date As String = dtActual.Rows(0).Item("Berthing_Date")
        Dim Berthing_Time As String = dtActual.Rows(0).Item("Berthing_TimeFrom")
        If (Berthing_Date.Trim().Length < 9) Then
            Try
                Dim days As String = Berthing_Date.Substring(0, 2)
                Dim Month As String = GetNameMonth(Berthing_Date.Substring(3, 2))
                Dim Year As String = "20" & Berthing_Date.Substring(6, 2)
                Berthing_Date = days & " " & Month & " " & Year
            Catch ex As Exception

            End Try
        End If
        Dim Depart_Actual_Date As String = dtLog_Out.Rows(0).Item("Last_Date")
        Dim Depart_Actual_Time As String = dtLog_Out.Rows(0).Item("Last_Time")
        If (Depart_Actual_Date.Trim().Length < 9) Then
            Try
                Dim days As String = Depart_Actual_Date.Substring(0, 2)
                Dim Month As String = GetNameMonth(Depart_Actual_Date.Substring(3, 2))
                Dim Year As String = "20" & Depart_Actual_Date.Substring(6, 2)
                Depart_Actual_Date = days & " " & Month & " " & Year
            Catch ex As Exception

            End Try
        End If

        Dim ts = DateDiff("n", Berthing_Date & " " & dtActual.Rows(0).Item("Berthing_TimeFrom"), Depart_Actual_Date & " " & dtLog_Out.Rows(0).Item("Last_Time")) ' in case you need it in minutes

        Dim StandartTIme As Double = (ts / 60).ToString("###################.00")
        Try
            If (CInt(StandartTIme.ToString.Split(".")(1)) > 49) Then
                StandartTIme = CDbl(StandartTIme.ToString.Split(".")(0)) + 1
            ElseIf (CInt(StandartTIme.ToString.Split(".")(1)) > 0) Then
                StandartTIme = CDbl(StandartTIme.ToString.Split(".")(0)) + 0.5
            End If
        Catch ex As Exception

        End Try

        dr.Item("Enter") = Berthing_Date & " " & Berthing_Time
        dr.Item("Sailing") = Depart_Actual_Date & " " & Depart_Actual_Time

        Dim tsx = DateDiff("n", Berthing_Date & " " & Berthing_Time, Depart_Actual_Date & " " & Depart_Actual_Time)
        Dim Spending As Double = (tsx / 60).ToString("###################.00")
        Try
            If (CInt(Spending.ToString.Split(".")(1)) > 49) Then
                Spending = CDbl(Spending.ToString.Split(".")(0)) + 1
            ElseIf (CInt(Spending.ToString.Split(".")(1)) > 0) Then
                Spending = CDbl(Spending.ToString.Split(".")(0)) + 0.5
            End If
        Catch ex As Exception

        End Try
        dr.Item("Spending") = Spending

        '********************************************
        dr.Item("Mooring") = "0"
        dr.Item("Port_Due") = "0"
        dr.Item("Berth_hire") = "0"
        dr.Item("Cargo") = "0"
        dr.Item("Cleanning") = "0"
        dr.Item("Water") = "0"
        dr.Item("Pilot") = "0"
        dr.Item("Working_unit") = "-"
        dr.Item("Working_Baht") = "0"
        dr.Item("Tug_unit") = "-"
        dr.Item("Tug_Bath4") = "0"
        dr.Item("Tug_Bathmore") = "0"
        dr.Item("Trans") = "0"
        dr.Item("Shifting") = "0"
        dr.Item("Tug_Gen") = "0"
        dr.Item("Tug_By") = "0"
        dr.Item("Total") = "0"


        Dim Total As Double = 0.0
        For Each dm As DataRow In dtValue.Rows
            Select Case (dm.Item("MATERIAL"))
                Case "000000002100000256"
                    dr.Item("Cleanning") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000240"
                    dr.Item("Shifting") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000238"
                    dr.Item("Trans") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000197"
                    dr.Item("Pilot") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "WP"
                    dr.Item("Water") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000268"
                    dr.Item("Cargo") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000281"
                    dr.Item("Cargo") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000001"
                    dr.Item("Berth_hire") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000200"
                    dr.Item("Port_Due") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000184"
                    dr.Item("Mooring") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000232"
                    dr.Item("Tug_Bathmore") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000198"
                    dr.Item("Shifting") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000234"
                    dr.Item("Tug_Gen") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000242"
                    dr.Item("Tug_By") = dm.Item("data4")
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000236"
                    dr.Item("Tug_Bath4") = dm.Item("data4")
                    Dim dtTug As DataTable = clsGet.Get_Booking_LogSheet_Tug(booking_no, "", "W")
                    Dim TugIn As Integer = 0
                    Dim TugOut As Integer = 0
                    For Each du As DataRow In dtTug.Rows
                        If (du.Item("Tug_Name") & "" <> "") Then
                            Dim dttugType As DataTable = clsGet.Get_TugBoat(du.Item("Tug_Name"))
                            If (dttugType.Rows.Count > 0) Then
                                If (dttugType.Rows(0).Item("Tug_Type") & "" = "0") Then
                                    If (du.Item("Type") & "" = "I") Then
                                        TugIn += 1
                                    Else
                                        TugOut += 1
                                    End If
                                End If
                            End If
                        End If
                    Next
                    dr.Item("Tug_unit") = TugIn & "-" & TugOut
                    Total += CDbl(dm.Item("data4"))
                Case "000000002100000237"
                    dr.Item("Working_Baht") = dm.Item("data4")
                    Dim dtTug As DataTable = clsGet.Get_Booking_LogSheet_Tug(booking_no, "", "W")
                    Dim TugIn As Integer = 0
                    Dim TugOut As Integer = 0
                    For Each du As DataRow In dtTug.Rows
                        If (du.Item("Tug_Name") & "" <> "") Then
                            Dim dttugType As DataTable = clsGet.Get_TugBoat(du.Item("Tug_Name"))
                            If (dttugType.Rows.Count > 0) Then
                                If Not (dttugType.Rows(0).Item("Tug_Type") & "" = "0") Then
                                    If (du.Item("Type") & "" = "I") Then
                                        TugIn += 1
                                    Else
                                        TugOut += 1
                                    End If
                                End If
                            End If
                        End If
                    Next
                    dr.Item("Working_unit") = TugIn & "-" & TugOut
                    Total += CDbl(dm.Item("data4"))
            End Select
        Next

        Dim dataTug As DataTable = clsGet.Get_Booking_LogSheet_Tug(booking_no, "", "W")
        Dim Tug As Integer = 0
        Dim Boat As Integer = 0
        Dim chkTug As Boolean = False
        For Each drTug As DataRow In dataTug.Rows
            If (drTug.Item("Tug_Name") & "" <> "") Then
                Dim dataTugType As DataTable = clsGet.Get_TugBoat(drTug.Item("Tug_Name"))
                If (dataTugType.Rows.Count > 0) Then
                    chkTug = True
                    If (dataTugType.Rows(0).Item("Tug_Type") & "" = "0") Then
                        Tug += 1
                    Else
                        If (drTug.Item("Tug_Event") & "" = "W") Then
                            Boat += 1
                        Else
                            Tug += 1
                        End If
                    End If
                End If
            End If
        Next
        Dim Store236 As Double = 0.0
        Dim Store237 As Double = 0.0
        If (chkTug) Then
            If (_236) And (Tug > 0) Then
                dr.Item("Tug_Bath4") = ComputeMoney("000000002100000236", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, Tug)
                Total += CDbl(dr.Item("Tug_Bath4"))
                Store236 = CDbl(dr.Item("Tug_Bath4"))
            ElseIf (_237) And (Boat > 0) Then
                dr.Item("Working_Baht") = ComputeMoney("000000002100000237", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, Boat)
                Total += CDbl(dr.Item("Working_Baht"))
                Store237 = CDbl(dr.Item("Working_Baht"))
            End If
        End If


        If (dtValue.Rows.Count = 0) Then
            Dim dtTug As DataTable = clsGet.Get_Booking_LogSheet_Tug(booking_no, "", "W")
            'Working
            Tug = 0
            Boat = 0
            Dim Hour_EXCEED As Integer = 0
            Dim Hour_EXCEED_Boat As Integer = 0
            Dim TugUnit As String = ""
            Dim BoatUnit As String = ""
            Dim C_In As Integer = 0
            Dim C_Out As Integer = 0
            Dim B_IN As Integer = 0
            Dim B_Out As Integer = 0
            For Each drr As DataRow In dtTug.Rows
                If (drr.Item("Tug_Name") & "" <> "") Then
                    Dim Hrs As String = drr.Item("Hrs")
                    Dim hours As Integer = CInt(Hrs.Split(":")(0))
                    If (hours > 3) Then
                        Hour_EXCEED += hours - 4
                        If (CInt(Hrs.Split(":")(1)) > 0) Then
                            Hour_EXCEED += 1
                        End If
                    End If
                    Dim dttugType As DataTable = clsGet.Get_TugBoat(drr.Item("Tug_Name"))
                    If (dttugType.Rows.Count > 0) Then
                        If (dttugType.Rows(0).Item("Tug_Type") & "" = "0") Then
                            Tug += 1
                            If (drr.Item("Type") & "" = "I") Then
                                C_In += 1
                            Else
                                C_Out += 1
                            End If
                        Else
                            If (drr.Item("Tug_Event") & "" = "W") Then
                                Boat += 1
                                If (drr.Item("Type") & "" = "I") Then
                                    B_IN += 1
                                Else
                                    B_Out += 1
                                End If
                            Else
                                Tug += 1
                                If (drr.Item("Type") & "" = "I") Then
                                    C_In += 1
                                Else
                                    C_Out += 1
                                End If
                            End If
                        End If
                    End If
                End If
            Next
            dr.Item("Working_unit") = B_IN & "-" & B_Out
            dr.Item("Tug_unit") = C_In & "-" & C_Out
            If (Hour_EXCEED > 0) Then
                dr.Item("Tug_Bathmore") = ComputeMoney("000000002100000232", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, Hour_EXCEED, "")
                Total += CDbl(dr.Item("Tug_Bathmore"))
            End If
            If (Tug > 0) Then
                dr.Item("Tug_Bath4") = ComputeMoney("000000002100000236", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, Tug)
                Total += CDbl(dr.Item("Tug_Bath4"))
                Total -= Store236
            End If
            If (Boat > 0) Then
                dr.Item("Working_Baht") = ComputeMoney("000000002100000237", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, Boat)
                Total += CDbl(dr.Item("Working_Baht"))
                Total -= Store237
            End If
        End If
        If (PORT = "B") Then
            dr.Item("Admis_Fee") = "0"
            dr.Item("Weighting") = "0"
            dr.Item("Hopper") = "0"
            dr.Item("WB") = "0"
            dr.Item("Other") = "0"
            Dim admis As Double = 0.0
            For Each dm As DataRow In dtValue.Rows
                Select Case (dm.Item("MATERIAL"))
                    Case "000000002100000255"
                        dr.Item("Weighting") = dm.Item("data4")
                        Total += CDbl(dm.Item("data4"))
                    Case "000000002100000104"
                        dr.Item("Hopper") = dm.Item("data4")
                        Total += CDbl(dm.Item("data4"))
                    Case "000000002100000282"
                        dr.Item("WB") = dm.Item("data4")
                        Total += CDbl(dm.Item("data4"))
                    Case "000000002100000097"
                        admis += CDbl(dm.Item("data4"))
                        Total += CDbl(dm.Item("data4"))
                    Case "000000002100000098"
                        admis += CDbl(dm.Item("data4"))
                        Total += CDbl(dm.Item("data4"))
                    Case "000000002100000099"
                        admis += CDbl(dm.Item("data4"))
                        Total += CDbl(dm.Item("data4"))
                    Case "000000002100000100"
                        admis += CDbl(dm.Item("data4"))
                        Total += CDbl(dm.Item("data4"))
                End Select
            Next
            dr.Item("Admis_Fee") = admis
            'เหลือ Other Services
            Dim dtOts As DataTable = clsGet.Get_OtherService_ItemBooking(booking_no)
            Dim Ots As Double = 0.0
            If (dt.Rows.Count > 0) Then
                For Each dk As DataRow In dtOts.Rows
                    Ots += dk.Item("Total")
                Next
            End If
            dr.Item("Other") = Ots
        End If


        If (ShipAgent) Then
            If (_184) Then
                dr.Item("Mooring") = ComputeMoney("000000002100000184", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, "")
                Total += CDbl(dr.Item("Mooring"))
            End If
            If (_200) Then
                dr.Item("Port_Due") = ComputeMoney("000000002100000200", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, "")
                Total += CDbl(dr.Item("Port_Due"))
            End If
            If (_001) Then
                dr.Item("Berth_hire") = ComputeMoney("000000002100000001", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, "")
                Total += CDbl(dr.Item("Berth_hire"))
            End If

            If (PORT = "L") Then
                If (_268) Then
                    dr.Item("Cargo") = ComputeMoney("000000002100000281", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, "")
                    Total += CDbl(dr.Item("Cargo"))
                End If
            Else
                If (_268) Then
                    dr.Item("Cargo") = ComputeMoney("000000002100000268", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, "")
                    Total += CDbl(dr.Item("Cargo"))
                End If
            End If
        Else
            If (_268) Then
                dr.Item("Cargo") = ComputeMoney("000000002100000268", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, "")
                Total += CDbl(dr.Item("Cargo"))
            End If
            If (_100) Then
                Dim dtTruck As DataTable = clsGet.Get_Load_Unload_Product_Bulk(booking_no, "", "")
                Dim Truck6 As Integer = 0
                Dim Truck810 As Integer = 0
                Dim Trailer As Integer = 0
                Dim Trailer2 As Integer = 0
                Dim CTtruck As Boolean = True
                Dim Admis As Double = 0
                For Each da As DataRow In dtTruck.Rows
                    Truck6 += CInt(da.Item("_Truck6"))
                    Truck810 += CInt(da.Item("_Truck810"))
                    Trailer += CInt(da.Item("_Trailer"))
                    Trailer2 += CInt(da.Item("_Trailer2"))
                Next
                If (Truck6 > 0) Then
                    Admis += CDbl(ComputeMoney("000000002100000099", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, Truck6))
                End If
                If (Truck810 > 0) Then
                    Admis += CDbl(ComputeMoney("000000002100000100", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, Truck810))
                End If
                If (Trailer > 0) Then
                    Admis += CDbl(ComputeMoney("000000002100000099", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, Trailer))
                End If
                If (Trailer2 > 0) Then
                    Admis += CDbl(ComputeMoney("000000002100000098", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, Trailer2))
                End If
                dr.Item("Admis_Fee") = Admis
                Total += Admis
            End If
            If (_255) Then
                dr.Item("Weighting") = ComputeMoney("000000002100000255", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, "")
                Total += CDbl(dr.Item("Weighting"))
            End If
            If (_104) Then
                Dim Hopper As DataTable = clsGet.Get_Load_Unload_Addition(booking_no)
                If Hopper.Rows.Count > 0 Then
                    If (CDbl(Hopper.Rows(0).Item("Hopper")) > 0) Then
                        dr.Item("Hopper") = ComputeMoney("000000002100000104", GRT, LOA, DWT, Draft, product_Amount, Customer, PORT, StandartTIme, CDbl(Hopper.Rows(0).Item("Hopper")))
                        Total += CDbl(dr.Item("Cargo"))
                    End If
                End If

            End If
        End If
        dr.Item("Total") = Total
        Dim dtw As DataTable = clsGet.Get_BookingWharf(booking_no)
        Dim WHarf As String = ""
        For Each dw As DataRow In dtw.Rows
            If WHarf = "" Then
                WHarf = dw.Item("Wharf")
            Else
                WHarf &= "," & dw.Item("Wharf")
            End If
        Next
        dr.Item("Wharf") = WHarf
        dt.Rows.Add(dr)
        dt.AcceptChanges()
        ViewState("Sum") = dt
    End Sub

    Public Function CreateDataset() As DataSet
        Dim ds As New DataSet
        Dim dt As New DataTable
        ds.Tables.Add(dt)
        dt.Columns.Add("id", GetType(String))
        dt.Columns.Add("MAT_NAME", GetType(String))
        dt.Columns.Add("name", GetType(String))
        dt.Columns.Add("data1", GetType(String))
        dt.Columns.Add("data2", GetType(String))
        dt.Columns.Add("data3", GetType(String))
        dt.Columns.Add("data4", GetType(String))
        ds.Tables(0).AcceptChanges()
        ds.AcceptChanges()
        Return ds
    End Function

    Private Function ComputeMoney(ByVal MATERIAL As String, ByVal GRT As String, ByVal LOA As String, ByVal DWT As String, ByVal Draft As String, ByVal ProductValue As String, ByVal Customer_no As String, ByVal PORT As String, ByVal _StandardTime As String, ByVal Valuexx As String)
        Dim dtRelation As DataTable = clsGet.Get_MeterailRelation(MATERIAL)
        Dim ds As New DataSet
        ds = CreateDataset()
        If (dtRelation.Rows.Count > 0) Then
            Dim Value As Double = 0.0
            Select Case dtRelation.Rows(0).Item("Relation")
                Case "01" 'สูตรคำนวนน้ำหนักเรือ GRT 
                    Value = GRT
                Case "02" 'L.O.A.
                    Value = LOA
                Case "03" 'DWT
                    Value = DWT
                Case "04" 'Draft
                    Value = Draft
                Case "05" 'Product
                    Value = ProductValue
                Case "06"
                    Value = 0.0
            End Select
            Dim conditioncheck As Boolean = True
            For Each drR As DataRow In dtRelation.Rows

                If (conditioncheck) Then
                    'ทำการเปรียบเทียบช่วงน่ำหนักเรือ
                    If (drR.Item("Compute_Between") & "" = "A") Then
                        'แบบแรกตกช่วงคิด
                        If ((CDbl(drR("Point_Start")) <= Value) And (CDbl(drR("Point_End")) >= Value)) Then
                            'Rate ตกในช่วง
                            Dim rate As String = drR.Item("Mat_Rate")
                            If (dtRelation.Rows(0).Item("Customer_no") & "" <> "") Then
                                Dim Cus() As String = dtRelation.Rows(0).Item("Customer_no").ToString().Split(",")
                                For i As Integer = 0 To Cus.Length - 1
                                    If (Cus(i).ToString = Customer_no) Then
                                        rate = dtRelation.Rows(0).Item("Customer_Rate").ToString().Split(",")(i)
                                        Exit For
                                    End If
                                Next
                            End If
                            Dim drNew As DataRow = ds.Tables(0).NewRow()
                            drNew.Item("id") = CInt(drR.Item("Meterial")).ToString()
                            drNew.Item("name") = drR.Item("Mat_Name")
                            drNew.Item("MAT_NAME") = ""  'ไม่ได้แสดง
                            If (CInt(Value).ToString() = "0") Then
                                If (dtRelation.Rows(0).Item("Relation") & "" <> "05") Then
                                    Value = 1.0
                                End If
                                drNew.Item("data1") = ""
                            Else
                                drNew.Item("data1") = Value
                            End If
                            Try
                                drNew.Item("data2") = rate
                                Dim newValue As Double = 0
                                If (drR.Item("Point_Unit") = "0") Then
                                    newValue = 1
                                Else
                                    newValue = (Value / CDbl(drR.Item("Point_Unit")))
                                End If
                                Dim StardardTime As Double = 0
                                If (drR("data3") = "S") Or (drR("data3") = "C") Then
                                    StardardTime = CDbl(_StandardTime)
                                End If
                                If (drR("Data3") = "N") Then
                                    drNew.Item("data3") = ""
                                    drNew.Item("data4") = (newValue * CDbl(rate)).ToString("###############0.00")
                                ElseIf (drR("Data3") = "S") Then
                                    Session("Lay_Time") = StardardTime
                                    drNew.Item("data3") = StardardTime
                                    drNew.Item("data4") = ((newValue * CDbl(rate)) * StardardTime).ToString("###############0.00")
                                Else
                                    If (drR.Item("Compute_Count") & "" = "") Then
                                        If (drR.Item("Compute_C") & "" = "D") Then
                                            If (StardardTime = 0) Then
                                                drNew.Item("data3") = 1
                                            Else
                                                drNew.Item("data3") = ((CDbl(StardardTime) \ 24) + 1).ToString()
                                            End If

                                        Else
                                            If (Value Mod CDbl(drR.Item("Point_Unit"))) <> 0 Then
                                                drNew.Item("data3") = (CInt(Value \ CDbl(drR.Item("Point_Unit"))) + 1).ToString()
                                            Else
                                                drNew.Item("data3") = CInt(Value \ CDbl(drR.Item("Point_Unit"))).ToString()
                                            End If
                                        End If
                                    Else
                                        If (Valuexx = "") Then
                                            drNew.Item("data3") = drR.Item("Compute_Count")
                                        Else
                                            drNew.Item("data3") = Valuexx
                                        End If
                                    End If

                                    drNew.Item("data4") = (CDbl(rate) * CDbl(drNew.Item("data3"))).ToString("###############0.00")
                                End If
                                If (drR.Item("LowRate") & "" = "") Then
                                    drR.Item("LowRate") = "0"
                                End If
                                If (CDbl(drR.Item("LowRate")) > 0.0) Then
                                    If (CDbl(drR.Item("LowRate")) > CDbl(drNew.Item("data4"))) Then
                                        'ราคาต่ำกว่า ราคาขั้นต่ำ
                                        If (drNew.Item("data3") = "") Then
                                            drNew.Item("data2") = drR.Item("LowRate")
                                            drNew.Item("data4") = drR.Item("LowRate")
                                        Else
                                            drNew.Item("data4") = drR.Item("LowRate")
                                            drNew.Item("data2") = CDbl(drR.Item("LowRate") / drNew.Item("data3")).ToString("################0.00")
                                        End If
                                    End If
                                End If
                            Catch ex As Exception
                                '((LOA-5)*Draft)+10%
                                If (drR.Item("LowRate") & "" = "") Then
                                    drR.Item("LowRate") = "0"
                                End If
                                Dim LoACompute As Double = ((LOA - 5.0) * Draft) + ((((LOA - 5.0) * Draft) * 10) / 100).ToString("##################0.00")
                                If (CDbl(drR.Item("LowRate")) > LoACompute) Then
                                    drNew.Item("data2") = drR.Item("LowRate")
                                    drNew.Item("data3") = "1"
                                    drNew.Item("data4") = drR.Item("LowRate")
                                Else
                                    drNew.Item("data2") = LoACompute.ToString()
                                    drNew.Item("data3") = "1"
                                    drNew.Item("data4") = LoACompute.ToString()
                                End If
                            End Try
                            ds.Tables(0).Rows.Add(drNew)
                            ds.Tables(0).AcceptChanges()
                            Exit For
                        End If
                    Else
                        'แบบสองผ่านช่วงไหนคิดหมด
                        If ((CDbl(drR("Point_Start")) <= Value)) Then
                            'Rate ตกในช่วง
                            Dim drNew As DataRow = ds.Tables(0).NewRow()
                            drNew.Item("id") = CInt(drR.Item("Meterial")).ToString()
                            drNew.Item("name") = drR.Item("Mat_Name")
                            drNew.Item("MAT_NAME") = "" 'ไม่ม่ชื่อ
                            Dim Nvalue As Double = 0
                            If (Value > drR("Point_End")) Then
                                Nvalue = drR("Point_End") - CDbl(drR("Point_Start"))
                            Else
                                Nvalue = Value - CDbl(drR("Point_Start"))
                            End If
                            Dim rate As String = drR.Item("Mat_Rate")
                            If (dtRelation.Rows(0).Item("Customer_no") & "" <> "") Then
                                Dim Cus() As String = dtRelation.Rows(0).Item("Customer_no").ToString().Split(",")
                                For i As Integer = 0 To Cus.Length - 1
                                    If (Cus(i).ToString = Customer_no) Then
                                        rate = dtRelation.Rows(0).Item("Customer_Rate").ToString().Split(",")(i)
                                        Exit For
                                    End If
                                Next
                            End If
                            drNew.Item("data1") = Nvalue
                            drNew.Item("data2") = rate

                            Dim newValue As Double = 0
                            If (drR.Item("Point_Unit") = "0") Then
                                newValue = 1
                            Else
                                newValue = (Nvalue / CDbl(drR.Item("Point_Unit")))
                            End If
                            Dim StardardTime As Double = 0
                            If (drR("data3") = "S") Or (drR("data3") = "C") Then
                                StardardTime = CDbl(_StandardTime)
                            End If
                            If (drR("Data3") = "N") Then
                                drNew.Item("data3") = ""
                                drNew.Item("data4") = (newValue * CDbl(rate)).ToString("###############0.00")

                            ElseIf (drR("Data3") = "S") Then
                                'ทำการคำนวณหา Stardard Time
                                'If (StardardTime = 0) Then
                                '    StardardTime = 1
                                'End If
                                Session("Lay_Time") = StardardTime
                                drNew.Item("data3") = StardardTime
                                drNew.Item("data4") = ((newValue * CDbl(rate)) * StardardTime).ToString("###############0.00")
                            Else
                                If (drR.Item("Compute_Count") & "" = "") Then
                                    If (drR.Item("Compute_C") & "" = "D") Then
                                        drNew.Item("data3") = (CDbl(StardardTime) \ 24).ToString()
                                    Else
                                        If (Value Mod CDbl(drR.Item("Point_Unit"))) <> 0 Then
                                            drNew.Item("data3") = (CInt(Value \ CDbl(drR.Item("Point_Unit"))) + 1).ToString()
                                        Else
                                            drNew.Item("data3") = CInt(Value \ CDbl(drR.Item("Point_Unit"))).ToString()
                                        End If
                                    End If
                                Else
                                    drNew.Item("data3") = drR.Item("Compute_Count")
                                End If

                                drNew.Item("data4") = (CDbl(rate) * CDbl(drNew.Item("data3"))).ToString("###############0.00")
                            End If
                            If (drR.Item("LowRate") & "" = "") Then
                                drR.Item("LowRate") = "0"
                            End If
                            If (CDbl(drR.Item("LowRate")) > 0.0) Then
                                If (CDbl(drR.Item("LowRate")) > CDbl(drNew.Item("data4"))) Then
                                    'ราคาต่ำกว่า ราคาขั้นต่ำ
                                    If (drNew.Item("data3") = "") Then
                                        drNew.Item("data2") = drR.Item("LowRate")
                                        drNew.Item("data4") = drR.Item("LowRate")
                                    Else
                                        drNew.Item("data4") = drR.Item("LowRate")
                                        drNew.Item("data2") = CDbl(drR.Item("LowRate") / drNew.Item("data3")).ToString("################0.00")
                                    End If
                                End If
                            End If

                            ds.Tables(0).Rows.Add(drNew)
                            ds.Tables(0).AcceptChanges()
                            Exit For
                        End If
                    End If
                End If
            Next
        Else
            'ไม่พบข้อมูลการผูกสูตร
            Dim Plant As String = "1104"
            If (PORT = "L") Then
                Plant = "1103"
            End If
            dtRelation = clsGet.Get_SubMeterail(MATERIAL)
            If (dtRelation.Rows.Count > 0) Then
                For Each dm As DataRow In dtRelation.Rows
                    If (Plant = dm.Item("WERKS")) Then
                        Dim drNew As DataRow = ds.Tables(0).NewRow()
                        Dim id As String = ""
                        Try
                            id = CInt(MATERIAL).ToString()
                        Catch ex As Exception
                            id = MATERIAL
                        End Try
                        drNew.Item("id") = id
                        drNew.Item("name") = ""
                        drNew.Item("MAT_NAME") = ""
                        drNew.Item("data1") = ""
                        drNew.Item("data2") = dm.Item("KBETR")
                        drNew.Item("data3") = "1"


                        drNew.Item("data4") = CDbl(dm.Item("KBETR") * CDbl("1")).ToString("###############0.00")
                        ds.Tables(0).Rows.Add(drNew)
                        ds.Tables(0).AcceptChanges()
                    End If
                Next
            End If
        End If
        If (ds.Tables(0).Rows.Count > 0) Then
            Return ds.Tables(0).Rows(0).Item("data4")
        Else
            Return "0"
        End If

    End Function

    Private Function GetNameMonth(ByVal Month As String) As String
        Try
            Select Case (CInt(Month).ToString("00"))
                Case "01"
                    Return "Jan"
                Case "02"
                    Return "Feb"
                Case "03"
                    Return "Mar"
                Case "04"
                    Return "Apr"
                Case "05"
                    Return "May"
                Case "06"
                    Return "Jun"
                Case "07"
                    Return "Jul"
                Case "08"
                    Return "Aug"
                Case "09"
                    Return "Sep"
                Case "10"
                    Return "Oct"
                Case "11"
                    Return "Nov"
                Case "12"
                    Return "Dec"
                Case Else
                    Return ""
            End Select
        Catch ex As Exception
            Return Month
        End Try

    End Function

    Private Function Create() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("Booking_no", GetType(String))
        dt.Columns.Add("Ship_name", GetType(String))
        dt.Columns.Add("Enter", GetType(String))
        dt.Columns.Add("Sailing", GetType(String))
        dt.Columns.Add("Wharf", GetType(String))
        dt.Columns.Add("Spending", GetType(String))
        dt.Columns.Add("LOA", GetType(String))
        dt.Columns.Add("GRT", GetType(String))
        dt.Columns.Add("Load", GetType(String))
        dt.Columns.Add("Disch", GetType(String))
        dt.Columns.Add("MT", GetType(String))
        dt.Columns.Add("Mooring", GetType(String))
        dt.Columns.Add("Port_Due", GetType(String))
        dt.Columns.Add("Berth_hire", GetType(String))
        dt.Columns.Add("Cargo", GetType(String))
        dt.Columns.Add("Cleanning", GetType(String))
        dt.Columns.Add("Water", GetType(String))
        dt.Columns.Add("Pilot", GetType(String))
        dt.Columns.Add("Working_unit", GetType(String))
        dt.Columns.Add("Working_Baht", GetType(String))
        dt.Columns.Add("Tug_unit", GetType(String))
        dt.Columns.Add("Tug_Bath4", GetType(String))
        dt.Columns.Add("Tug_Bathmore", GetType(String))
        dt.Columns.Add("Trans", GetType(String))
        dt.Columns.Add("Total", GetType(String))
        dt.Columns.Add("DWT", GetType(String))
        dt.Columns.Add("Shifting", GetType(String))
        dt.Columns.Add("Tug_Gen", GetType(String))
        dt.Columns.Add("Tug_By", GetType(String))
        dt.Columns.Add("Agent", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function
End Class